import Vue from 'vue'
import VueRouter from 'vue-router'
// import Layout from '../views/layout/Layout.vue'

Vue.use(VueRouter)

// let paths = '/'
// let names = ''
const routes = [
  { path: '/', redirect: 'login' },
  { path: '/login', component: () => import('../views/login/Login.vue') },
  {
    path: '/layout',
    name: '首页',
    component: () => import('../views/layout/Layout.vue'),
    redirect: '/home',
    children: [
      { path: '/home', component: () => import('../views/home/Home.vue') },
      {
        path: '/pms',
        name: '商品',
        redirect: '/pms/product',
        component: () => import('../views/goods/Pms.vue'),
        children: [
          { path: '/pms/product', name: '商品列表', component: () => import('../views/goods/GoodsList.vue') },
          { path: '/pms/addProduct', name: '添加商品', component: () => import('../views/goods/AddGoods.vue') },
          { path: '/pms/productCate', name: '商品分类', component: () => import('../views/goods/ProductCate.vue') },
          { path: '/pms/updateProductCate', name: '商品分类编辑', component: () => import('../views/goods/UpdateProductCate.vue') },
          { path: '/pms/productAttr', name: '商品类型', component: () => import('../views/goods/ProductAttr.vue') },
          { path: '/pms/brand', name: '商品类型', component: () => import('../views/goods/Brand.vue') },
          { path: '/pms/updateBrand', name: '商品类型', component: () => import('../views/goods/UpdateBrand.vue') },
        ],
      },
      {
        path: '/oms',
        name: '订单',
        component: () => import('../views/order/Oms.vue'),
        redirect: '/oms/order',
        children: [
          { path: '/oms/order', name: '订单列表', component: () => import('../views/order/OrderList.vue') },
          { path: '/oms/orderDetail', name: '订单详情', component: () => import('../views/order/OrderDetail.vue') },
          { path: '/oms/orderSetting', name: '订单设置', component: () => import('../views/order/OrderSetting.vue') },
        ],
      },
    ],
  },
]
const router = new VueRouter({
  routes,
})
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err)
}

// router.beforeEach((to, from, next) => {
//   // console.log(to)
//   // console.log(from)
//   let paths = to.path
//   let names = to.path.slice(1)
//   // console.log(names)
//   let ro = {
//     path: paths,
//     name: names,
//     component: () => import('../views/' + names + '/' + names.charAt(0).toUpperCase() + names.slice(1) + '.vue'),
//   }
//   // routes.push(ro)

//   // 得到所有路由表信息 getRoutes
//   let flag = router.getRoutes().some((item) => item.name == names)
//   if (flag) {
//     next()
//   } else {
//     router.addRoute(ro)
//     next({ ...ro, replace: true })
//   }
// })

export default router
